package com.medaxis.hpams.sso.controller;


import com.medaxis.hpams.dto.global.BaseResponse;
import com.medaxis.hpams.dto.global.ErrorException;
import com.medaxis.hpams.dto.global.SuccessResponse;
import com.medaxis.hpams.sso.constant.Constants;
import com.medaxis.hpams.sso.domain.Role;
import com.medaxis.hpams.sso.domain.dto.RoleDto;
import com.medaxis.hpams.sso.service.RoleService;
import com.medaxis.hpams.sso.utils.ResponseUtils;
import com.medaxis.hpams.sso.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 */
@RestController
@RequestMapping(Constants.VERSION_NUMBER+"/sso/role")
@Api(tags = Constants.VERSION_NUMBER+ "角色管理")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private ResponseUtils responseUtils;

    @ApiOperation(value = "通过角色信息查找(角色名称，类型，状态，权限字符串)",httpMethod = "GET")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功",response = RoleDto.class)})
    @GetMapping("/list")
    public BaseResponse list(RoleDto role) {
        startPage();
        List<RoleDto> list = roleService.selectRoleList(role);
        return new SuccessResponse(getDataTable(list));
    }

    /**
     * 修改保存角色
     */
    @ApiOperation(value = "修改角色信息",httpMethod = "PUT")
    @ApiResponses({@ApiResponse(code = 200, message = "修改成功",response = int.class)})
    @PutMapping("/update")
    public BaseResponse edit(@Validated @RequestBody RoleDto role) {
        roleService.checkRoleAllowed(role);
        if (Constants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            throw new ErrorException("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (Constants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            throw new ErrorException("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
//        else if (role.getDeptIds().length > 1) {
//            throw new ErrorException("请选择单个部门");
//        }
//        role.setUpdateBy(SecurityUtils.getUsername());
        return responseUtils.toResponse(roleService.updateRole(role));
    }

    /**
     * 新增角色
     */
    @ApiOperation(value = "添加角色信息",httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200, message = "添加成功",response = int.class)})
    @PostMapping("/add")
    public BaseResponse add(@Validated @RequestBody RoleDto role) {
        if (Constants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            throw new ErrorException("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (Constants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            throw new ErrorException("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
//        else if (role.getDeptIds().length > 1) {
//            throw new ErrorException("请选择单个部门");
//        }
//        role.setCreateBy(SecurityUtils.getUsername());
        return new SuccessResponse(roleService.insertRole(role));

    }

    /**
     * 状态修改
     */
    @ApiOperation(value = "变更角色状态",httpMethod = "PUT")
    @ApiResponses({@ApiResponse(code = 200, message = "变更成功",response = int.class)})
    @PutMapping("/changeStatus/{roleId}")
    public BaseResponse changeStatus(@PathVariable String roleId) {
        RoleDto role=(RoleDto) roleService.selectRoleById(Long.valueOf(roleId));
        roleService.checkRoleAllowed(role);
//        role.setUpdateBy(SecurityUtils.getUsername());
        return responseUtils.toResponse(roleService.updateRoleStatus(role));
    }

    /**
     * 删除角色
     */
    @ApiOperation(value = "根据id批量删除角色信息",httpMethod = "DELETE")
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功",response = int.class)})
    @DeleteMapping("/{roleIds}")
    public BaseResponse remove(@PathVariable Long[] roleIds) {
        return responseUtils.toResponse(roleService.deleteRoleByIds(roleIds));
    }

//    @ApiOperation(value = "导出角色信息到本地",notes = "返回导出文件名称",httpMethod = "GET")
//    @ApiResponses({@ApiResponse(code = 200, message = "导出成功",response = String.class)})
//    @GetMapping("/export")
//    public BaseResponse export(RoleDto role) {
//        List<RoleDto> list = roleService.selectRoleList(role);
//        ExcelUtil<RoleDto> util = new ExcelUtil<RoleDto>(RoleDto.class);
//        return new SuccessResponse(util.exportExcel(list, "角色数据").get("msg"));
//    }





}